package com.fridge.setting;

import com.fridge.data.DBHelper;
import com.fridge.data.ShoppingItem;
import com.fridge.fridgeList.AddFridgeItemActivity;
import com.id2216.fridge.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.NumberPicker;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker.OnValueChangeListener;

public class SettingFragment extends Fragment
{
	
	NumberPicker notificationNumber;
	CheckBox allowNotification;
	private Button fbButton;
	
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
    	
    	
        return inflater.inflate(R.layout.setting_fragment, container, false); 
    } 
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        
        if( isVisibleToUser == true)
        {
        	SharedPreferences prefs = getActivity().getSharedPreferences("smartFridgePrefs",Context.MODE_PRIVATE);   
        	String email = prefs.getString("allowNotification", "YES");
        	
        	if( allowNotification != null)
        	{
            	if( email != null & email.compareTo("YES") == 0 )
            		allowNotification.setChecked(true);
            	else
            		allowNotification.setChecked(false);        		
        	}
        	
        	if( notificationNumber != null)
        	{
            	int dayCount = prefs.getInt("notificationDay", 2);
                notificationNumber.setValue(dayCount);        		
        	}
        	

        }
    }
       
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState); 
        
        notificationNumber = (NumberPicker) getActivity().findViewById(R.id.noficationDayPicker);
        allowNotification = (CheckBox) getActivity().findViewById(R.id.allowNotification);
        
        notificationNumber.setValue(2);
        notificationNumber.setMinValue(1);
		notificationNumber.setMaxValue(30);
		
		
		
    	SharedPreferences prefs = getActivity().getSharedPreferences("smartFridgePrefs",Context.MODE_PRIVATE);   
    	String email = prefs.getString("allowNotification", "YES");
    	
    	if( allowNotification != null)
    	{
        	if( email != null & email.compareTo("YES") == 0 )
        		allowNotification.setChecked(true);
        	else
        		allowNotification.setChecked(false);        		
    	}
    	
    	if( notificationNumber != null)
    	{
        	int dayCount = prefs.getInt("notificationDay", 2);
            notificationNumber.setValue(dayCount);        		
    	}

		
		
		notificationNumber.setOnValueChangedListener(new OnValueChangeListener() {
		    @Override
		    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		    	
		    	if( oldVal != newVal )
		    	{
	            	SharedPreferences prefs = getActivity().getSharedPreferences("smartFridgePrefs",Context.MODE_PRIVATE);
	            	SharedPreferences.Editor editor = prefs.edit();
	            	editor.putInt("notificationDay", newVal); 
	            	editor.commit(); 

		    	}
		    }
		});
		
		allowNotification.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
            	
            	SharedPreferences prefs = getActivity().getSharedPreferences("smartFridgePrefs",Context.MODE_PRIVATE);
            	SharedPreferences.Editor editor = prefs.edit();
            	if(isChecked == true)
            		editor.putString("allowNotification", "YES"); 
            	else
            		editor.putString("allowNotification", "NO"); 

            	editor.commit(); 
            	
            }
		});
		
		fbButton = (Button) getActivity().findViewById(R.id.button_fb);
		fbButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {            	
            	
        		Intent fbIntent = new Intent();
        		fbIntent.setClass(getActivity().getBaseContext(), LoginActivity.class);        		
        		getActivity().startActivity(fbIntent);
            }
        });

    }
}
